package cn.hzking.gjmall.wxapi;

import android.graphics.Bitmap;

import java.io.ByteArrayOutputStream;

/**
 * Created by Administrator on 2017/6/5.
 */

public class WxShareUtil {
    public static byte[] compressImage2ByteArray(Bitmap image, boolean needRecycle) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        int quality = 100;

        while (baos.toByteArray().length / 1024 >= 32) {
            if (quality <= 0) {
                return baos.toByteArray();
            }
            baos.reset();
            // Some formats, like PNG which is lossless, will ignore the quality setting
            image.compress(Bitmap.CompressFormat.JPEG, quality, baos);
            quality -= 10;
        }
        if (needRecycle) {
            image.recycle();
        }
        return baos.toByteArray();
    }
}
